package com.example.dell.boradcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.example.dell.saver.AiService;
import com.example.dell.saver.BackService;

/**
 * 开机自启服务
 */
public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String action_boot="android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(action_boot)){
            startMessagListeningeService(context);
        }

    }

    private void startMessagListeningeService(Context context) {
        Intent service = new Intent();
        service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        service.setClass(context,AiService.class);

        //根据版本不同，开启不同的
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(service);
        }else {
            context.startService(service);
        }

    }
}
